package cn.iocoder.yudao.module.shop.revenue.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.module.api.trade.order.message.TradeAfterSaleCompletedMessage;
import cn.iocoder.yudao.module.api.trade.order.message.TradeOrderCompletedMessage;
import cn.iocoder.yudao.module.api.trade.order.message.TradeOrderPaidMessage;
import cn.iocoder.yudao.module.api.trade.scan.message.ScanOrderPaidMessage;
import cn.iocoder.yudao.module.shop.revenue.service.core.CoreRevenueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 商家营收 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class RevenueConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(TradeOrderPaidMessage message) {
        log.info("[revenue][onMessage][交易订单支付完成({})]", message.getOrder().getNo());
        SpringUtil.getBean(CoreRevenueService.class).createWhenTradeOrderPaid(message.getOrder());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(TradeOrderCompletedMessage message) {
        log.info("[revenue][onMessage][交易订单交易完成({})]", message.getOrder().getNo());
        SpringUtil.getBean(CoreRevenueService.class).updateWhenTradeOrderCompleted(message.getOrder());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(TradeAfterSaleCompletedMessage message) {
        log.info("[revenue][onMessage][交易订单售后完成({})]", message.getOrder().getNo());
        SpringUtil.getBean(CoreRevenueService.class).updateWhenTradeAfterSaleCompleted(message.getOrder(), message.getOrderItem());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ScanOrderPaidMessage message) {
        log.info("[revenue][onMessage][扫码订单支付完成({})]", message.getOrder().getTradeNo());
        SpringUtil.getBean(CoreRevenueService.class).createWhenScanOrderPaid(message.getOrder());
    }

}
